package com.mycompany.j2eem.service.wrjservice;

import com.huaban.analysis.jieba.JiebaSegmenter;
import com.huaban.analysis.jieba.SegToken;
import com.mycompany.j2eem.Entity.bean.PictureInDBBean;
import com.mycompany.j2eem.Entity.bean.PictureObjectBean;
import com.mycompany.j2eem.Entity.bean.TagMapperBean;
import com.mycompany.j2eem.Mapper.wrjmapper.RecommendationMapper;
import com.mycompany.j2eem.Mapper.wrjmapper.SearchMapper;
import me.xiaosheng.util.Segment;
import me.xiaosheng.word2vec.Word2Vec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.OverridesAttribute;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.SplittableRandom;

/**
 * @author wurj
 * @time 2019/5/14 15:00
 * @description
 */
@Service
public class SearchService {
    @Autowired
    SearchMapper serachMapper;

    @Autowired
    RecommendationMapper recommendationMapper;

    @Autowired
    ModelService modelService;

    static Double THRESHOLD=0.20;

    public List<String> searchForTag(String query){
        System.out.println(query);
        List<String> tagsFromQuery=split(query);
        for (String s:tagsFromQuery){
            System.out.println("分词:"+s);
        }
        List<TagMapperBean> tagsInDB=serachMapper.getTags();
        List<String> simTags=calculate(tagsFromQuery,tagsInDB);
        return simTags;
    }

    public List<List<PictureInDBBean>> searchForPics(List<String> tags){
        List<List<PictureInDBBean>> pictureInDBS=new ArrayList<>();
        for (String s:tags){
            List<PictureInDBBean> pictureInDB=serachMapper.getPictures(s);
            pictureInDBS.add(pictureInDB);
        }
        return pictureInDBS;
    }

    //分词
    private List<String> split(String s){
        JiebaSegmenter jiebaSegmenter=new JiebaSegmenter();
        List<SegToken> list=jiebaSegmenter.process(s, JiebaSegmenter.SegMode.SEARCH);
        List<String> names=new ArrayList<>();
        for (SegToken segToken:list){
            names.add(segToken.getWord());
        }
        return names;
    }

    //获取父亲标签
    public List<String> getPointedTags(boolean isRoot){
        List<String> tags=new ArrayList<>();
        List<TagMapperBean> tagMapperBeans=serachMapper.getTags();
        System.out.println(tagMapperBeans.size());
        List<TagMapperBean> pointedTags=new ArrayList<>();
        for (TagMapperBean tagMapperBean:tagMapperBeans){
            String id;
            if (isRoot){
                id=tagMapperBean.getId();
                List<String> parent=serachMapper.getTagNameIdFromParent(id);
                System.out.println("parent "+parent);
                if (parent.size()==0){
                    pointedTags.add(tagMapperBean);
                    System.out.println(tagMapperBean);
                }
            }else {
                id=tagMapperBean.getId();
                String child=serachMapper.getTagNameIdFromChild(id);
                System.out.println("child "+child);
                if (child==null){
                    pointedTags.add(tagMapperBean);
                    System.out.println(tagMapperBean);
                }
            }
        }
        for (TagMapperBean tagMapperBean:pointedTags){
            tags.add(tagMapperBean.getTagname());
            System.out.println(tagMapperBean.getTagname());
        }
        return tags;
    }


    //计算相似度，返回高相似度的tags
    private List<String> calculate(List<String> tagsFromQuery, List<TagMapperBean> tagsInDB){
        List<String> highSimTags=new ArrayList<>();
        Word2Vec word2Vec=modelService.getWord2Vec();
        for (TagMapperBean db:tagsInDB){
            List<String> dbSentence= split(db.getTagname());
            System.out.println(db.getTagname());
            float simSentence=word2Vec.sentenceSimilarity(tagsFromQuery,dbSentence);
            System.out.println("相似度"+"   句子"+simSentence);
            if (simSentence>=THRESHOLD){
                highSimTags.add(db.getId());
                System.out.println("添加"+db.getId());
            }
        }
        return highSimTags;
    }

    //返回下一级子标签
    public List<String> getRelationalTags(String parentTag) {
        String id=serachMapper.getTagId(parentTag);
        System.out.println(id);
        List<String> childTag=serachMapper.getChildren(id);
        List<String> tags=new ArrayList<>();
        for (String s:childTag){
            String a=serachMapper.getChildName(s);
            System.out.println(a);
            tags.add(a);
        }
        return tags;
    }

    public List<PictureInDBBean> hotFilter(List<PictureInDBBean> hotPictureList) {
        List<PictureInDBBean> avail=new ArrayList<>();
        for (PictureInDBBean pictureInDBBean:hotPictureList){
            String authorize=serachMapper.getAuthorize(pictureInDBBean.getId());
            System.out.println("权限："+authorize);
            if (authorize.equals("public")&&!avail.contains(pictureInDBBean)){
                avail.add(pictureInDBBean);
            }
        }
        return avail;
    }

    public List<PictureInDBBean> personalRecFilter(List<PictureObjectBean> recommendPictureList) {
        List<PictureInDBBean> pictureInDBBeans=new ArrayList<>();
        for (PictureObjectBean s:recommendPictureList){
            if (serachMapper.getAuthorize(s.getPictureId())=="public"){
                pictureInDBBeans.add(serachMapper.getPicture(s.getPictureId()));
            }
        }
        return pictureInDBBeans;
    }

    public List<PictureInDBBean> TagFilter(List<List<PictureInDBBean>> pictureInDBS) {
        List<PictureInDBBean> avail=new ArrayList<>();
        for (List<PictureInDBBean> pictureInDBBeans:pictureInDBS){
            for (PictureInDBBean p:pictureInDBBeans){
                String authorize=serachMapper.getAuthorize(p.getId());
                System.out.println("权限："+authorize);
                if (authorize.equals("public")&&!avail.contains(p)){
                    avail.add(p);
                }
            }
        }
        return avail;
    }

    public List<String> getTagNames(List<String> availTags) {
        List<String> names=new ArrayList<>();
        for (String s:availTags){
            String a=serachMapper.getTagName(s);
            System.out.println(a);
            names.add(a);
        }
        return names;
    }

    public List<String> searchForPointedTag(String tag) {
        String a=serachMapper.getTagId(tag);
        List<String> tagsInDB=new ArrayList<>();
        tagsInDB.add(a);

        return tagsInDB;
    }
}